import stylecloud
from tools.getDataBase import get_conn


#pillow与python解释器版本冲突，无法显示词云，使用的对应函数被废弃
# 实现绘制标题词云
def getTitleImg(field, icon_name, output_name):
    sql = f'select {field} from movies'
    conn, cursor = get_conn()
    cursor.execute(sql)
    data = cursor.fetchall()
    text1 = ','.join([row[0] for row in data])
    text2 = ','.join(text1)
    stylecloud.gen_stylecloud(text=text2, icon_name=icon_name,
                              output_name=output_name,
                              font_path='/static/font/simhei.ttf')


# 实现绘制演员词云图
def getCastsImg(field, icon_name, output_name):
    sql = f'select {field} from movies'
    conn, cursor = get_conn()
    cursor.execute(sql)
    data = cursor.fetchall()
    text1 = ','.join([row[0] for row in data])
    text2 = ','.join(text1)
    stylecloud.gen_stylecloud(text=text2, icon_name=icon_name,
                              output_name=output_name,
                              font_path='/static/font/simhei.ttf')



# 实现绘制自定义词云图
def getCommentsImg(field, searchWord, icon_name, output_name):
    sql = f'select {field} from comments where movieName="{searchWord}"'
    conn, cursor = get_conn()
    cursor.execute(sql)
    data = cursor.fetchall()
    text1 = ','.join([row[0] for row in data])
    text2 = ','.join(text1)
    stylecloud.gen_stylecloud(text=text2, icon_name=icon_name,
                              output_name=output_name,
                              font_path='/static/font/simhei.ttf')
